package vg.modules.navigator.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import vg.services.data_base_manager.data.skeleton.GraphTreeElement;
import vg.services.main_manager.MainManager;
import vg.services.plugin_manager.event_and_request.request.UIRequestSelectTab;
import vg.services.user_interface_manager.interfaces.AUserInterface;

/**
 * This class realizes menu item for selecting of tab.
 * @author tzolotuhin
 */
public class SelectionTabMenuItem implements IMenuItem {
	private final JMenuItem jItem;
	private final SelectionTabAction action;
	/**
	 * Constructor.
	 * @param tree - tree of graph nodes.
	 * @param userInterface - main user interface.
	 */
	public SelectionTabMenuItem(final JTree tree, final AUserInterface userInterface) {		
		this.action = new SelectionTabAction(tree, userInterface);
		this.jItem = new JMenuItem("Go to this tab");
		final Icon icon = new ImageIcon("data/resources/textures/goto.png");
		this.jItem.setIcon(icon);
		this.jItem.addActionListener(this.action);
	}
	public JMenuItem getMenuItem() {
		return(this.jItem);
	}
	public boolean isVisible() {
		return(this.action.isVisible());
	}
	public void updateTheme() {
		SwingUtilities.updateComponentTreeUI(this.jItem);
	}
}
/**
 * Selects tab in desktop.
 * @author tzolotuhin
 */
class SelectionTabAction implements ActionListener {
	private final JTree tree;
	private final AUserInterface userInterface;
	/**
	 * Constructor.
	 * @param tree - tree of graph nodes.
	 */
	public SelectionTabAction(final JTree tree, final AUserInterface userInterface) {
		this.tree = tree;
		this.userInterface = userInterface;
	}
	/**
	 * Action
	 */
	public void actionPerformed(ActionEvent e) {
		MainManager.log.printDebug("[" + this.getClass().getName()+".actionPerformed] [PROCESS] Select tab(from navigator).");
		TreePath[] p = tree.getSelectionPaths();
		if(p != null && p.length == 1) {
			MainManager.log.printDebug("[" + this.getClass().getName()+".actionPerformed] [OK] Select tab(from navigator).");
			TreeModel tm = this.tree.getModel();
			if(tm == null) {
				MainManager.log.printDebug("[" + this.getClass().getName()+".actionPerformed] [BAD] Select tab(from navigator). Model = null.");
				return;
			} else {
				GraphTreeElement node = ((GraphTreeElement)tm.getRoot()).vg_getNode(p[0]);
				if((node.vg_getType() & GraphTreeElement.DEF_TAB_TYPE) > 0) {
					UIRequestSelectTab request = new UIRequestSelectTab(node.vg_getTabId(), null);
					this.userInterface.addRequest(request);
				} else {
					MainManager.windowMessage.warningMessage("Selection should be tab, if you want to select this tab", "Select tab");				
				}
			}
		} else {
			MainManager.log.printDebug("[" + this.getClass().getName()+".actionPerformed] [BAD] Select tab(from navigator).");
			MainManager.windowMessage.warningMessage("Selection should have one node, if you want to select this tab", "Select tab");
		}	
	}
	public boolean isVisible() {
		if(this.tree != null) {
			TreePath[] p = this.tree.getSelectionPaths();
			if(p != null && p.length == 1) {
				TreeModel tm = this.tree.getModel();
				if(tm != null) {
					GraphTreeElement node = ((GraphTreeElement)tm.getRoot()).vg_getNode(p[0]);
					if((node.vg_getType() & GraphTreeElement.DEF_TAB_TYPE) > 0) {
						return true;
					}
				}
			}
		}
		return false;
	}
}
